package logic.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.shwread.http.util.Logger;

import java.util.ArrayList;
import java.util.List;

import logic.bean.Area;
import logic.db.DBHelper;
import logic.table.AreaTable;
import logic.table.AreaTable.AreaColumns;

/**
 * 全国省/市/区县信息 数据访问类
 * @author 刘宾  
 * @date 2015年4月7日 下午2:28:41
 */
public class AreaDao
{
	private Context              context;
	private static AreaDao       dao;
	private DBHelper             dbHelper;
	
	private AreaDao(Context context) {
		this.context = context;
		dbHelper = DBHelper.getInstance(context);
	}
	
	public static AreaDao getInstance(Context context) {
		dao = new AreaDao(context);
		return dao;
	}
	
	/**
	 * 根据父级id查询区域列表
	 * @param parentId
	 */
	public List<Area> query(int parentId){
		List<Area> data = new ArrayList<Area>();
		String selection = AreaColumns.PARENT_ID + " = ? ";
		String[] selectionArgs = new String[] { String.valueOf(parentId) };
		Cursor c = dbHelper.getSqliteDatabaseR().query(AreaTable.table, null, selection, selectionArgs, null, null, null);
		try
		{
			if (null != c)
			{
				while (c.moveToNext())
				{
					Area bean = dbHelper.getObject(Area.class, c);
					data.add(bean);
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			dbHelper.closeCursor(c);
		}
		return data;
	}
	/**
	 * 根据类型查询区域列表
	 * @param areaType
	 */
	public List<Area> queryByType(int areaType){
		List<Area> data = new ArrayList<Area>();
		String selection = AreaColumns.AREA_TYPE + " = ? ";
		String[] selectionArgs = new String[] { String.valueOf(areaType) };
		Cursor c = dbHelper.getSqliteDatabaseR().query(AreaTable.table, null, selection, selectionArgs, null, null, null);
		try
		{
			if (null != c)
			{
				while (c.moveToNext())
				{
					Area bean = dbHelper.getObject(Area.class, c);
					data.add(bean);
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			dbHelper.closeCursor(c);
		}
		return data;
	}
	
	/**
	 * 批量添加城市信息
	 * @param list
	 */
	public void addList(List<Area> list){
		try
        {
	        dbHelper.getSqliteDatabaseW().beginTransaction();
	        for (Area area : list)
	        {
	        	ContentValues values = dbHelper.getContentValues(DBHelper.TYPE_INSERT, area);
	        	dbHelper.getSqliteDatabaseW().insert(AreaTable.table, null, values);
	        }
	        dbHelper.getSqliteDatabaseW().setTransactionSuccessful();
	        dbHelper.getSqliteDatabaseW().endTransaction();
        }
        catch (Exception e)
        {
        	Logger.e("添加城市列表失败,areaType:"+list.get(0).areaType,e);
	        e.printStackTrace();
        }
	}
	
	/**
	 * 删除所有数据
	 * @return
	 */
	public int deleteAll(){
		try
        {
	        return dbHelper.getSqliteDatabaseW().delete(AreaTable.table, null, null);
        }
        catch (Exception e)
        {
        	Logger.e("删除数据失败",e);
	        e.printStackTrace();
	        return 0;
        }
	}
	
	
	public long queryCount(){
		long count = 0;
		String sql = "select count(" + AreaColumns.COLUMN_ID + ") from " + AreaTable.table ;
		Cursor c = dbHelper.getSqliteDatabaseR().rawQuery(sql, null);
		try
		{
			if (null != c)
			{
				c.moveToNext();
				count = c.getLong(0);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			dbHelper.closeCursor(c);
		}
		return count;
	}
}
